//
//  LZBasePresenter.m
//  laziz_Consumer
//
//  Created by XMac on 16/12/12.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZBasePresenter.h"
#import "LZAlertView.h"
#import "AppDelegate.h"
#import "LZLoginViewController.h"
#import "LZUserModel.h"

@implementation LZBasePresenter

#pragma mark - 校验状态码
+ (BOOL)isRequestStatusCodeCorrect:(id)data fail:(PresenterCommonFail)fail {
    if ([data[@"statusCode"] integerValue] == kCodeSuccesss) {
        return YES;
    }
    else {
//        NSString *str = data[@"statusCode"];
//        fail(str);
//        return NO;
        
        DLog(@"接口请求失败：\n%@",data);
        NSString *errorCode = data[@"statusCode"];
        [LZBasePresenter checkAuthorStatus:errorCode];
        fail(errorCode);
        return NO;
    }
}

#pragma mark - 校验用户信息状态
+ (BOOL)checkAuthorStatus:(NSString*)status {
    if ([status integerValue] == kCodeAuthorExpired) {
        // 用户未登录或者登陆过期
        
        [[LZUserModel Instance] logOut];
        
        LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"mag_account_expired", nil) andImage:nil cancelButtonTitle:LZLocalizedString(@"cancel", nil) otherButtonTitles:LZLocalizedString(@"confirm", nil),nil];
        [alert show];
        
        alert.clickButtonIndexBlock = ^(NSInteger index) {
            if (index == 1) {
                [LZBasePresenter pushLoginViewController];
            }
        };
        return NO;
    }
    else if ([status integerValue] == kCodeAuthorForbidden) {
        // 该用户已被停用，请联系管理员
        
        [[LZUserModel Instance] logOut];
        
        LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"mag_account_stopstate", nil) andImage:nil cancelButtonTitle:nil otherButtonTitles:LZLocalizedString(@"confirm", nil),nil];
        [alert show];
        return NO;
    }
    
    return YES;
}

#pragma mark - 进入登录页操作
+ (void)pushLoginViewController {
    AppDelegate *appleDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    UITabBarController *tabController = (UITabBarController*)appleDelegate.window.rootViewController;
    UINavigationController *navController = tabController.selectedViewController;
    UIViewController *lastVC = navController.viewControllers.lastObject;
    if (![lastVC isKindOfClass:[LZLoginViewController class]]) {
        LZLoginViewController * vc = [[LZLoginViewController alloc] init];
        [navController pushViewController:vc animated:YES];
    }
}

@end
